package itsm.isperp.framework.web.controller;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.domain.CountQuery;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.workflow.domain.BaseProcessBean;
import itsm.isperp.workflow.service.BpmProcessRunService;
import itsm.isperp.workflow.utils.BpmConst;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 基础控制器
 * 
 * @author lizx
 * @date 2013-2-17
 * @version V1.0
 */
@Component
public abstract class BaseProcessController<T extends BaseProcessBean> extends
		BaseController<T> {

	public abstract String getProceessKey();

	public static final String TODO_TASK = "待办任务";
	public static final String HAVETODO_TASK = "我的已办";
	public static final String START_TASK = "我的发起";
	public static final String DRAFT_TASK = "草稿";

	/**
	 * 获取流程的他哦你估计查询
	 * 
	 * @return CountQuery
	 */
	protected CountQuery getCountQuery() {
		return this.createDefaultFlowCountQuery();
	}

	/**
	 * 获取流程的他哦你估计查询
	 * 
	 * @return CountQuery
	 */
	protected CountQuery getCountQuery(String username) {
		return this.createDefaultFlowCountQuery("'" + username + "'");
	}

	protected CountQuery createDefaultFlowCountQuery() {
		CountQuery cq = createDefaultCountQuery();
		cq.addCountFilter(TODO_TASK, true,
				BpmConst.getQueryMyTaskSqlCondition(getProceessKey()));
		cq.addCountFilter(HAVETODO_TASK,
				BpmConst.getQueryMyTaskAttendSqlCondition(getProceessKey()));
		cq.addCountFilter(START_TASK,
				BpmConst.getQueryMyTaskStartedSqlCondition(getProceessKey()));
		cq.addCountFilter(DRAFT_TASK,
				new StringBuffer(" ( process_id IS NULL AND  creator ='")
						.append(ContextHolder.getLoginUsername()).append("') ")
						.toString());
		return cq;
	}

	protected CountQuery createDefaultFlowCountQuery(String username) {
		CountQuery cq = createDefaultCountQuery();
		cq.addCountFilter(TODO_TASK, true,
				BpmConst.getQueryTaskSqlCondition(getProceessKey(), username));
		cq.addCountFilter(HAVETODO_TASK, BpmConst
				.getQueryMyTaskAttendSqlCondition(getProceessKey(), username));

		return cq;
	}

	@RequestMapping("processMenu")
	@ResponseBody
	public ITreeNode[] processMenu() {
		return this.getCountQuery().getTreeNodes();

	}

	@RequestMapping("list")
	@ResponseBody
	@Override
	public PageResponse<T> list(JqGridRequest request, HttpServletRequest req) {
		return this.getList(getCountQuery(), request, req);
	}

	/**
	 * 流程代办的统计数
	 * 
	 * @return
	 */
	@RequestMapping("approvalCount")
	@ResponseBody
	public Long approvalCount() {
		return this.getCountQuery().getTreeNodeCount(TODO_TASK);

	}

	/**
	 * 流程代办的统计数
	 * 
	 * @return
	 */
	@RequestMapping("approvalCountByUsername")
	@ResponseBody
	public Long approvalCountByUsername(String username) {
		return this.getCountQuery(username).getTreeNodeCount(TODO_TASK);

	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		return this.getListDetail(getCountQuery(), request, req);
	}

	@RequestMapping("listDetailByUsername")
	@ResponseBody
	public PageResponse<Map<String, Object>> listDetailByUsername(
			JqGridRequest request, HttpServletRequest req, String username) {
		return this.getListDetail(getCountQuery(username), request, req);
	}

	/**
	 * 根据唯一标识删除数据，并在删除前结束次业务的流程
	 */
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultMessage delete(String id, HttpServletRequest req) {
		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");
		try {
			bpmProcessRunService.endProcessByBusinessId(
					ContextHolder.getLoginUsername(), id, "删除数据");
		} catch (Exception e) {
			logger.error(e);
		}
		this.getService().delete(id);
		return ResultMessage.newInstance(true);
	}

}
